<?php

namespace app\test\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\facade\View;
class Ad extends Controller
{
    //跳转添加页面
    public function add()
    {
        Log::record('广告添加');
        return View::fetch('add');
    }
//广告添加方法
    public function create(Request $request)
    {
        //接值
       $arr=$request->post();
        $file=$request->file('img');
        $info=$file->move('./uploads');
        $image = \think\Image::open('./uploads/'.$info->getSavename());
       // 给原图左上角添加水印并保存text_image.png
        $image->text('week3',getcwd().'/simfang.ttf',20,'#ffffff')->save('./uploads/'.$info->getSavename());
        $arr['img']='/uploads/'.$info->getSavename();
        $res=Db::table('ad')->insert($arr);
        if ($res){
//           $this->success('添加成功','show');
            return json(['code'=>200,'msg'=>'添加成功','data'=>$res]);
        }else{
            $this->success('添加失败','add');
        }
    }
//广告展示
    public function show(Request $request)
    {
        $data=Db::table('ad')->order('sort','asc')->paginate(3);
        return View::fetch('show',['data'=>$data]);
    }
//批量删除
    public function del(Request $request)
    {
        $id=$request->post('id');
        $status=$request->post('status');
//        if ($status==1){
//            return json(['code'=>400,'msg'=>'不能删除']);
//        }
        $res=Db::table('ad')->where(['id'=>$id,'status'=>1])->delete();
        if ($res){
            return json(['code'=>200,'msg'=>'删除成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'删除失败','data'=>'']);
        }
    }
    //前台广告
    public function save(Request $request)
    {
        if ($request->isPost()){
             return josn(['code'=>200,'msg'=>'success','data'=>1]);
        }
        return View::fetch('save');
    }

}
